/*
 * TCSS 445 - Major Project
 * 
 * Author: Chris Davidson
 * Author: Dallas Rosson
 */
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import model.Patron;

/**
 * PatronDataInputFrame
 * 
 * The frame used to get the data for a new patron from the user.
 *
 * @author Dallas Rosson
 * @author Chris Davidson
 * @version 1.0, 28 May 2012
 */
public class PatronDataInputFrame {
    
    /**
     * The frame.
     */
    private final JFrame my_frame = new JFrame();
    
    /**
     * The patron.
     */
    private final Patron my_patron;
    
    /**
     * The name text input field.
     */
    private JTextField my_nameTxt = new JTextField();
        
    /**
     * The address text input field.
     */
    private JTextField my_addressTxt = new JTextField();
        
    /**
     * The state text input field.
     */
    private JTextField my_stateTxt = new JTextField();
        
    /**
     * The zip text input field.
     */
    private JTextField my_zipTxt = new JTextField();
        
    /**
     * The email text input field.
     */
    private JTextField my_emailTxt = new JTextField();
        
    /**
     * Constructs a new PatronDataInputFrame.
     * 
     * @param the_patron The patron to store the data in.
     */
    public PatronDataInputFrame(final Patron the_patron) {
        
        my_patron = the_patron;
    }
    
    /**
     * Initializes and instantiates the frame.
     */
    public void initialize() {
        
        my_frame.setPreferredSize(new Dimension(350, 250));
        my_frame.setResizable(false);
        my_frame.setVisible(true);
        my_frame.setTitle("Newport Oldtown Library Catalog - New Patron Input");
        my_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        JPanel mainPanel = new JPanel();
        
        JLabel nameLbl = new JLabel("Name:    ");
        nameLbl.setPreferredSize(new Dimension(75, 25));
        
        JLabel addressLbl = new JLabel("Address: ");
        addressLbl.setPreferredSize(new Dimension(75, 25));
        
        JLabel stateLbl = new JLabel("State:   ");
        stateLbl.setPreferredSize(new Dimension(75, 25));
        
        JLabel zipLbl = new JLabel("Zip:     ");
        zipLbl.setPreferredSize(new Dimension(75, 25));
        
        JLabel emailLbl = new JLabel("Email:   ");
        emailLbl.setPreferredSize(new Dimension(75, 25));
        
        my_nameTxt.setPreferredSize(new Dimension(220, 25));
        my_nameTxt.setBorder(BorderFactory.createLineBorder(Color.black));
        
        my_addressTxt.setPreferredSize(new Dimension(220, 25));
        my_addressTxt.setBorder(BorderFactory.createLineBorder(Color.black));
        
        my_stateTxt.setPreferredSize(new Dimension(220, 25));
        my_stateTxt.setBorder(BorderFactory.createLineBorder(Color.black));
        
        my_zipTxt.setPreferredSize(new Dimension(220, 25));
        my_zipTxt.setBorder(BorderFactory.createLineBorder(Color.black));
        
        my_emailTxt.setPreferredSize(new Dimension(220, 25));
        my_emailTxt.setBorder(BorderFactory.createLineBorder(Color.black));
        
        mainPanel.setLayout(new GridLayout(6, 1));
        
        JPanel namePnl = new JPanel();
        namePnl.setLayout(new FlowLayout());
        
        JPanel addressPnl= new JPanel();
        addressPnl.setLayout(new FlowLayout());
        
        JPanel statePnl = new JPanel();
        statePnl.setLayout(new FlowLayout());
        
        JPanel zipPnl = new JPanel();
        zipPnl.setLayout(new FlowLayout());
        
        JPanel emailPnl = new JPanel();
        emailPnl.setLayout(new FlowLayout());
        
        namePnl.add(nameLbl);
        namePnl.add(my_nameTxt);
        addressPnl.add(addressLbl);
        addressPnl.add(my_addressTxt);
        statePnl.add(stateLbl);
        statePnl.add(my_stateTxt);
        zipPnl.add(zipLbl);
        zipPnl.add(my_zipTxt);
        emailPnl.add(emailLbl);
        emailPnl.add(my_emailTxt);
        
        mainPanel.add(namePnl);
        mainPanel.add(addressPnl);
        mainPanel.add(statePnl);
        mainPanel.add(zipPnl);
        mainPanel.add(emailPnl);
        
        JButton createPatronBtn = new JButton("Create Patron");
        
        createPatronBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent the_e) {
                
                my_patron.setName(my_nameTxt.getText());
                my_patron.setAddress(my_addressTxt.getText());
                my_patron.setState(my_stateTxt.getText());
                my_patron.setZip(Integer.parseInt(my_nameTxt.getText()));
                my_patron.setEmail(my_emailTxt.getText());
                
                my_frame.dispose();
            }
        });
        
        JButton cancelBtn = new JButton("Cancel");
        
        cancelBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent the_e) {
                
                my_frame.dispose();
            }
        });
        
        JPanel buttonPnl = new JPanel();
        buttonPnl.add(createPatronBtn);
        buttonPnl.add(cancelBtn);
        
        mainPanel.add(buttonPnl);
        
        my_frame.add(mainPanel);
        
        
        my_frame.pack();
    }
    
}
